{include file="admin@public/header" /}

<link href="/public/static/admin/blue/bills.css?ver=20210803202401" rel="stylesheet" type="text/css">

<script src="/public/static/admin/js/common/base.js"></script> 
<style>

.qtyInsert{margin-left: 8px;font-weight: 100;font-size: 12px;color: #fff;background-color: #B1B1B1;padding: 0 5px;border-radius: 2px;line-height: 19px;height: 20px;display: inline-block; }
.qtyInsertAfter{margin-right: 8px;font-weight: 100;font-size: 12px;color: #fff;background-color: #23B317;padding: 0 5px;border-radius: 2px;line-height: 19px;height: 20px;display: inline-block;}
</style>


<div class="wrapper">

  <div class="mod-toolbar-top mr0 cf dn" id="toolTop"></div>
  <div class="bills cf">
    <div class="con-header">
      <dl class="cf">
        <dd>
          <label>单据日期：</label>

           <input type="date" id="date" class="ui-input " value="{$today}" style="width: 125px;">
        
        </dd>
   
          <dd id="identifier"  style="float:right">
          <label>单据编号:</label>
          <span id="number">DB20210809001</span>
          <i id="editBills"></i>
          <span class="ui-combo-wrap" id="numberAuto" style="display: none;">
            <i class="trigger"></i>
          </span>
        </dd>
      </dl>
      <hr class="hrcls" />
    </div>

    <div id="storageBoxA" class="shadow target_box dn pcnone" style="position: absolute;  z-index: 999 !important; display: block;"><ul><li data-id="129127919158566" data-name="默认仓库">CK001 默认仓库</li></ul></div>

    <div id="storageBoxB" class="shadow target_box dn pcnone" style="position: absolute; z-index: 999 !important; display: block;"><ul><li data-id="129127919158566" data-name="默认仓库">CK001 默认仓库1</li></ul></div>

    <!--  以隐藏的方式获取联系地址的样式 ，作用是样式 -->
   <div id="pelem_goods"  style="display:none;">
    <div class="pr" id="elem_goods"><input type="text" class="textbox  customelement" name="goodsname" autocomplete="off" ><span class="ui-icon-ellipsis"></span></div>
   </div>

  <!--  以隐藏的方式获取联系地址的样式 ，作用是赋值 -->
  <div style="display:none;">
     <input type="text" id="goodsname" value=""   autocomplete="off" readonly="">
     <input type="text" id="ck1name" value=""   autocomplete="off" readonly="">
     <input type="text" id="ck2name" value=""   autocomplete="off" readonly="">
     
  <!--   <input type="text" id="goodsid" value=""   autocomplete="off" readonly="">
    <input type="text" id="goodstype" value=""   autocomplete="off" readonly="">
    <input type="text" id="goodsmark" value=""   autocomplete="off" readonly="">
     <input type="text" id="goodsdw" value=""   autocomplete="off" readonly=""> -->
  </div>


  <div id="p_ck1" style="display: none"></div>
  <div id="p_ck2" style="display: none"></div>
    <!-- 仓库弹窗 -->
  <div class="ui-droplist-wrap" id="chooseBatch" style="position: absolute; top: 231.891px; z-index: 1000; width: 102px; display: none; left: 617px;">
    <div style="position: fixed;width: 100%;height: 100%;top: 0;left: 0;background-color: rgba(0,0,0,0);" onclick="closeBatch()"></div>
    <div class="droplist" style="position: relative; overflow: auto; min-height: 150px; height: 150px;">
      {volist name="store" id="vo"}
      <div class="list-item" ck_id="{$vo.id}" title="{$vo.name}">{$vo.name}</div>
      {/volist}
    </div>
  </div>


    <div class="wrapper_con">

      <span id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a></span>
      <div class="grid-wrap">
        <table id="grid">
        </table>
        <div id="page"></div>
      </div>   
      <div class="cf" id="bottomField">
        <div class="fr" id="toolBottom">
          <span id="groupBtn">
            <a id="add" class="ui-btn ui-btn-sp mrb">新增</a>
            <a id="copy" class="ui-btn">复制</a>
            <div class="ui-btn-menu mr10">
              <a class="ui-btn-bill ui-btn-hl menu-btn mrb" href="#" id="print" target="_blank">打印<b></b></a>
              <div class="con hl more-operate-con" style="margin-left: 0px;font-size: 14px;width: 54px;padding: 3px;">
                <ul class="more-operate cf" style="border-style:dashed; border-width:1px; border-color:#ccc;">
                  <li style="padding-left: 8px;">
                    <a href="#" id="printSet">设置</a>
                  </li>
                </ul>
              </div>
            </div>
            <a id="edit" class="ui-btn">保存</a>
            <a id="SN_export" class="ui-btn">导出SN</a>
            <a class="ui-btn" id="audit">审核</a>
          </span>
        </div>
      </div>
      <div id="mark"></div>
    </div>
    <div class="con-footer cf">
      <div class="mb10">
          <textarea type="text" id="note" class="ui-input ui-input-ph" placeholder="暂无备注信息"></textarea>
      </div>
      <ul id="user-field">
        <li class="udfLink"><a class="user-field" onclick="customField()">自定义字段</a></li>
      </ul>
      

      <div class="cf fr">
        <a href="#" class="linkToCheck ui-btn-bill ui-btn-add mr20" onclick="addPage('/store/move/index.html','storage','调拨单记录')">历史单据</a>
        <a href="#" id="operaLog" class="ui-btn">操作日志</a>
      </div>       
    </div>
  
  </div>
</div>


<style type="text/css">
  .pcnone{display: none !important;}

</style>


<!-- <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> -->


<script type="text/javascript">
  $(document).ready(function () {
    // 获取当前日期
		var myDate = new Date;
		var year = myDate.getFullYear(); //获取当前年
		var mon = myDate.getMonth() + 1; //获取当前月
		var date = myDate.getDate(); //获取当前日
		// console.log(year + '-' + mon + '-' + date)
		if (mon < 10) {
			mon = "0" + mon;
		}
		if (date < 10) {
			date = "0" + date;
		}
		$("#date").val(year + '-' + mon + '-' + date);

    //获取商品
    $(".grid-wrap").on("click", ".ui-icon-ellipsis", function (e) {
      pre_choose_goods('/admin/basic/choose_goods.html', this);

    });
  });
  function pre_choose_goods(url, obj) {
    choose_goods(url, obj, function (res) {
      if (res.btn == 3 && res.status == 1) {
        //console.log(res);
        var data = res.data;
        if (data.status == 1) {

          var $count=data.info.count;
          var good_list=data.info.list;
          var allIds = $("grid").jqGrid('getDataIDs');
          var ids =  $("#grid").getDataIDs();
          
          // console.log("当前行");
          // console.log(row_id);
          // console.log("长度："+ids.length);
          // console.log(ids);
          row_id=parseInt(row_id);
          var $cha_id = ids.length-row_id;//最后id-当前id
          var cha_han= $count-$cha_id-1;//需要补充的行
          //console.log("cha:"+cha_han);
          //补充行
          if(cha_han>0){
                var mydata1 = [];
                for (var iii = ids.length; iii < ids.length+cha_han; iii++) {
                  var obj = { id: iii + 1, goodsname: "", goodstype: "", goodsmark: "", dw: "", shuliang: "", ck1: "", ck2: "", ck3: "", mark: "", goodsid: "0", ck1_id: "0", ck2_id: "0" }
                  mydata1.push(obj);
                }
                console.log(mydata1);
                for (var iii = 0; iii <= mydata1.length; iii++) {
                  jQuery("#grid").jqGrid('addRowData', ids.length+iii+1, mydata1[iii]);
                }

          }

          for (let i = 0; i <$count; i++) {

            $("#grid").jqGrid("saveCell",edit_row,edit_col);
            $("#grid").jqGrid("setCell",row_id+i,'goodsname',good_list[i].name);
            $("#grid").jqGrid("setCell",row_id+i,'goodstype',good_list[i].goodstype);
            $("#grid").jqGrid("setCell",row_id+i,'goodsmark',good_list[i].mark);
            $("#grid").jqGrid("setCell",row_id+i,'dw',good_list[i].dw);
            $("#grid").jqGrid("setCell",row_id+i,'goodsid',good_list[i].id);
            $("#grid").jqGrid("setCell",row_id+i,'shuliang',1.00);
          }
          var shuliangs = parseFloat($("#grid").getCol('shuliang', false, 'sum')).toFixed(2);
          $("#grid").footerData('set', { "goodsname": '合计：', shuliang: shuliangs});
          

        } else {
          layer.msg("获取商品信息失败");
        }
      }
    });


  }
</script>


<script type="text/javascript">
  var edit_row=0;
  var edit_col=0;
  var row_id=0;
  var col_name=0;
  $(document).ready(function () {
    var outhtml='';
    var inhtml=' ';
    var  col2='<span class="red">*</span>商品<label> -- 扫描枪录入</label><span id="barCodeInsert" onclick="smq();" class="close"><span class="ui-icon-circle"></span></span>';
    var  col7='<span id="outstore" onclick="outstore();"><span class="red">*</span>调出仓库<small id="batchStorageA">批量</small>'+outhtml+'</span>';
    var  col8='<span id="outstore" onclick="instore();"><span class="red">*</span>调入仓库<small id="batchStorageB">批量</small>'+inhtml+'</span>';
  
    $("#grid").jqGrid({

        datatype: "local",//数据来源，本地数据（local，json,jsonp,xml等）
        height: '90%',//高度，表格高度。可为数值、百分比或'auto'
        colNames: ['','操作',col2, '商品类别','商品备注', '单位', '<span class="red">*</span>数量',col7,col8,'可用仓库（调出仓）','备注','商品ID','仓库1ID','仓库2ID'],
        colModel: [
          { name: 'id', index: 'id', width: 110, hidden:true},
         // { name: 'goodsname', width: 300, editable: true},
         { name: '', index: 'id', width: 60,label: "操作", formatter: cLink },
          
          {
              name: "goodsname",
              label: "商品",
              width: 300,
              title: !0,
            
              classes: "goods",
              editable: true,
              edittype: "custom",
              editoptions: {
                custom_element: function(t, e) {
                    //return $(".goodsAuto")[0]
                    var ht=$("#elem_goods");
                    $("#pelem_goods").html('<div class="pr" id="elem_goods"><input type="text" class="textbox  customelement" name="goodsname" autocomplete="off" ><span class="ui-icon-ellipsis"></span></div>');
                    return ht;
                },
                custom_value: function(t, e, a) {
                      var ad = $("#goodsname").val();
                      $("#goodsname").val("");//重置赋值
                      return ad;
                },
                handle: function() {
                  
                },
                trigger: "ui-icon-ellipsis"
              }
          },


          { name: 'goodstype', width: 100 , editable: false},
          { name: 'goodsmark', width: 100 , editable: false},
          { name: 'dw', width: 100 ,  editable: false},
          { name: 'shuliang', width: 100,  editable: true, align:"right" },
          {
            name: "ck1",
            width: 100,
            link: "inventory",
            editable: true,
            edittype: "custom",
            editoptions: {
              custom_element: function(t, e) {
                $("#p_ck1").html('<div class="pr" id="ck1"><input type="text" class="textbox storageAuto customelement ui-combo-active" name="locationName" autocomplete="off" id="1_locationName" style="outline: -webkit-focus-ring-color auto 5px;"><span class="ui-icon-triangle-1-s"></span></div>');
                return $("#ck1");
              },
              custom_value: function (t, e, a) {
                var ba = $("#ck1name").val();
                return ba;
              },
            
              trigger: "ui-icon-triangle-1-s"
            }
          },
          {
            name: "ck2",
            width: 120,
            link: "inventory",
            editable: true,
            edittype: "custom",
            editoptions: {
              custom_element: function(t, e) {
                $("#p_ck2").html('<div class="pr" id="ck2"><input type="text" class="textbox storageAuto customelement ui-combo-active" name="locationName" autocomplete="off" id="1_locationName" style="outline: -webkit-focus-ring-color auto 5px;"><span class="ui-icon-triangle-1-s"></span></div>');
                return $("#ck2");
              },
              custom_value: function (t, e, a) {
                var ba = $("#ck2name").val();
                return ba;
              },
            
              trigger: "ui-icon-triangle-1-s"
            }
          },
          { name: 'ck3', width: 120 ,  editable: false},
          { name: 'mark', width: 100 ,  editable: true},
          { name: 'goodsid', width: 100 , hidden: true},
          { name: 'ck1_id', width: 100 , hidden: true},
          { name: 'ck2_id', width: 100 , hidden: true},
        ],

        cellEdit:true,
        cellurl : '/',
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: 'id',
        sortorder: "desc",
        pagerpos: "left",//分页位置
        rownumbers:true,
        pgbuttons: true,//翻页按钮
        pagination: true,
        altRows: !0,
        gridview: !0,
        shrinkToFit: !1,
        cellLayout: 8,
        autowidth: !0,
        pager: "#page",
        viewrecords: !0,
        cmTemplate: {
          sortable: !1,
          title: !1
        },
        loadError: function (t, e, i) {
          parent.Public.tips({
            type: 1,
            content: "操作失败了哦，请检查您的网络链接！"
          })
        },
        beforeEditCell:function(rowid,cellname,value,iRow,iCol){
            col_name=cellname;
            edit_row=iRow;
            edit_col=iCol;
            row_id=rowid;
            var gname=$("#grid").jqGrid("getCell",row_id,'goodsname');
            $("#goodsname").val(gname);

            var ck1name=$("#grid").jqGrid("getCell",row_id,'ck1');
            $("#ck1name").val(ck1name);
            var ck2name=$("#grid").jqGrid("getCell",row_id,'ck2');
            $("#ck2name").val(ck2name);
        },
        beforeSaveCell: function (rowid, cellname, value, iRow, iCol) {
            edit_row = iRow;
            edit_col = iCol;
            row_id = rowid;

            
            if (cellname == 'shuliang') {
              var shuliangs = parseFloat($("#grid").getCol('shuliang', false, 'sum')).toFixed(2);
            }
            // 重新设置合计的数量
            $("#grid").footerData('set', { "goodsname": '合计', shuliang: shuliangs});
        },
        // 合计
        footerrow: true,
        gridComplete: function () {
          var shuliangs = parseFloat($("#grid").getCol('shuliang', false, 'sum')).toFixed(2);
          $("#grid").footerData('set', { "goodsname": '合计：', shuliang: shuliangs});
        }
        

      });

      var mydata = [];
      for (var i = 0; i < 5; i++) {
        var obj = {id: i+1, goodsname: "", goodstype: "", goodsmark: "", dw: "", shuliang: "", ck1: "", ck2: "", ck3: "", mark: "",goodsid:"0",ck1_id:"0",ck2_id:"0"}
        mydata.push(obj);
      };
      for ( var i = 0; i <= mydata.length; i++){
        jQuery("#grid").jqGrid('addRowData', i + 1, mydata[i]);
      }
     

    
  });

  //增加、删除按钮
  function cLink(cellvalue, options, rowObject) {
      return '<div class="operating"><span class="ui-icon ui-icon-plus" title="新增行" onclick="add_row(' + options.rowId + ')" style="margin:0 2px;"></span> <span class="ui-icon ui-icon-trash" onclick="del_row(' + options.rowId + ')" title="删除行" style="margin:0 2px;">删除</span></div>';
    }

  // 自定义字段
  function customField(){
    layer.open({
      type: 2,
      title: "设置自定义字段",
      shadeClose: true,
      shade: 0.3,
      maxmin: false, //开启最大化最小化按钮
      area: ['554px', '430px'],
      fixed: false, //不固定
      btn: ['确定', '取消'],
      content: "/admin/basic/custom_field.html",
      btn1: function (index, layero) {
        
        layer.close(index);
        return false;
      },
      btn2: function (index, layero) {
        layer.close(index);
        return false;
      }
    })
  }
    
   

</script>


<script type="text/javascript">

    // 选择仓库弹窗
    $(".grid-wrap").on("click", ".ui-icon-triangle-1-s", function (e) {
      var top = $(this).offset().top;
      var left = $(this).offset().left;
      $("#chooseBatch").show();
      $("#chooseBatch").css({"top":(top + 2)+"px","left":(left - 100)+"px"});
    })
    // 仓库选择鼠标移入移出
    $(".ui-droplist-wrap .droplist .list-item").hover(function(){
      $(this).addClass("on");
    },function(){
      $(this).removeClass("on");
    })

    $(".ui-droplist-wrap .droplist").on("click",".list-item",function(e){
      var title=$(this).attr("title");
      //仓库1
      if (col_name=="ck1") {
        var ck1_id=$(this).attr("ck_id");
        $("#ck1name").val(title);
        $("#grid").jqGrid("setCell",row_id,'ck1_id',ck1_id);
        $("#grid").jqGrid("saveCell", edit_row, edit_col);
        edit_row = 0;
        edit_col = 0;
      }
      //仓库2
      if (col_name=="ck2") {
        var ck2_id=$(this).attr("ck_id");
        $("#ck2name").val(title);
        $("#grid").jqGrid("setCell",row_id,'ck2_id',ck2_id);
        $("#grid").jqGrid("saveCell", edit_row, edit_col);
        edit_row = 0;
        edit_col = 0;
      }

      $("#chooseBatch").hide();
    
    });

    function outstore(){

        if($("#storageBoxA").hasClass("pcnone")) {
          if(!$("#storageBoxB").hasClass("pcnone")) {
              $("#storageBoxB").addClass('pcnone');
          }
          $("#storageBoxA").removeClass('pcnone');//
          var top=getposition('batchStorageA',1);
          var left=getposition('batchStorageA',2);
          $("#storageBoxA").css({'left':(left-81)+'px','top':(top + 5)+'px'});
        }else{
          $("#storageBoxA").addClass('pcnone');  
        }    
         
         
    }  

    function instore(){
        if($("#storageBoxB").hasClass("pcnone")) {
          if(!$("#storageBoxA").hasClass("pcnone")) {
              $("#storageBoxA").addClass('pcnone');//
          }
          $("#storageBoxB").removeClass('pcnone');//removeClass
          var top=getposition('batchStorageB',1);
          var left=getposition('batchStorageB',2);
          $("#storageBoxB").css({'left':(left-81)+'px','top':(top + 5)+'px'});
        }else{
          $("#storageBoxB").addClass('pcnone');  
        }      
    }   

    function smq(){

        if($("#barCodeInsert").hasClass("close")) {
           $("#barCodeInsert").addClass('open');  
           $("#barCodeInsert").removeClass('close');//removeClass
         
        }else{
           if($("#barCodeInsert").hasClass("open")) {
             $("#barCodeInsert").addClass('close');  
             $("#barCodeInsert").removeClass('open');//removeClass
           }   
        }   
        
    }   
</script>

<script type="text/javascript">

  var grid=$('#grid');

    // 保存按钮
    $(".wrapper").on("click", "#save", function (e) {
      save_jqgrid(edit_row,edit_col,grid,function(res){
          $("#grid").jqGrid().trigger("reloadGrid");
          layer.msg("保存成功");
      });
    })
    

    $(".wrapper").on("click", "#savaAndAdd", function(e) {
        //先保存
        $("#grid").jqGrid().trigger("reloadGrid");
        if(edit_row != 0 && edit_col != 0){
          // $("#grid").jqGrid("saveCell",edit_row,edit_col);
          edit_row = 0;
          edit_col = 0;
        }
         savedata();
       

    })

    function savedata(){
       var movedata = $('#grid').jqGrid('getGridParam','data');
    
        movedata.forEach(function (self, index) {
            console.log(self);
            if (self['goodsid']!="0") {
                if(self['ck1_id']=="0"){
                   layer.msg("请选择调出仓库");
                   return false;
                }
                if(self['ck2_id']=="0"){
                   layer.msg("请选择调入仓库");
                   return false;
                }
                if(self['shuliang']=="0"){
                   layer.msg("请输入商品数量");
                   return false;
                }
            }
        });

        var time= $("#date").val();
        var sn= $("#number").html();
        var mark=$("#note").val();
        $.ajax({
          type: 'POST',
          url: "save.html",
          data: {time:time,sn:sn,mark:mark,movedata:movedata},
          dataType: "json",
          async: false,
          error: function (request) {
            layer.msg("发送请求失败！");
          },
          success: function (data) {
            if (data.status == 1) {
              layer.msg(data.msg);
            } else {
              layer.msg(data.msg);
            }

          }
        });

    }


</script>



{include file="admin@public/footer" /}
